package com.lixiang.userinfo.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.lixiang.userinfo.entity.Account;
import com.lixiang.userinfo.service.IAccountService;
import com.lixiang.userinfo.util.ApiResult;
import com.lixiang.userinfo.util.ResultCode;

/**
 * 
 * 账户信息控制器
 * @author 李湘
 * @date 2018-10-20
 */
//证明是controller层并且返回json
@RestController
public class AccountController {
	
    //日志记录器
	private static Logger logger = LoggerFactory.getLogger(AccountController.class);
	
	@Autowired
	private IAccountService accountService;//自动注入依赖
	
	/**
	 * 查询账户密码（用于登录匹配账户密码）
	 * @param accName 账户名称
	 * @param accPwd  账户密码
	 * @return
	 */
	@RequestMapping(value="account/login")
	@ResponseBody
	public ApiResult selectNamePwd(String accName,String accPwd,HttpServletRequest request){
		try {
			//组织查询参数
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("accName", accName);
			map.put("accPwd", accPwd);
			
			Account account=accountService.selectNamePwd(map);
			if(account != null){
				HttpSession session = request.getSession();
				session.setAttribute("isLogin", "yes");
				session.setAttribute("authLevel", account.getRoleId());
				session.setAttribute("accName", accName);
				return ApiResult.success(account);
			}else{
				return ApiResult.failure(ResultCode.USER_LOGIN_ERROR);
			}
		} catch (Exception e) {
			logger.error("服务器正忙="+e.getMessage());
			return ApiResult.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	

}
